package com.furongji.education.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.furongji.common.result.Result;
import com.furongji.education.domain.po.Banner;
import com.furongji.education.service.BannerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author furongji
 * @since 2025-05-24
 */
@Tag(name = "轮播图前台管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/courses")
public class BannerApiController {

    private final BannerService bannerService;

    private final RedisTemplate<String, Object> redisTemplate;

    @Operation(summary = "获取轮播图")
    @GetMapping("/findAllBanner")
    public Result getBannerList() {
        String cacheKey = "bannerList";
        List<Banner> cacheData = (List<Banner>) redisTemplate.opsForValue().get(cacheKey);
        if (cacheData != null) {
            return Result.success(cacheData);
        }
        LambdaQueryWrapper<Banner> bannerWrapper = new LambdaQueryWrapper<>();
        bannerWrapper.orderByDesc(Banner::getId).last("limit 3");
        List<Banner> bannerList = bannerService.list(bannerWrapper);
        redisTemplate.opsForValue().set(cacheKey, bannerList, 10, TimeUnit.MINUTES);
        return Result.success(bannerList);
    }
}
